// TDPSOLA_Console.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "TDPSOLA_Console.h"
#include "Psola.h"
#include "TextHandle.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		if (argc != 4)
		{
			cout << "usage: " << argv[0];
			cout << " <source> / <F0> / <Speaking Rate> " << endl;
			cout << endl;
			return FALSE;
		}

		char cBuff[256];
		GetCurrentDirectory(256,cBuff);  // lay thu muc hien tai, truyen vao cBuff ; thaodv-070111
		
		CTextHandle::CsDirectoryOfFile = cBuff;		
		// Database la HalfSyl.dat; thaodv-070111
		CTextHandle::CS_DBFileName = CTextHandle::CsDirectoryOfFile + '\\' + "HalfSyl.dat";

	
		if(!CPsola::DB_Open(CTextHandle::CS_DBFileName))
		{
			cout << "Database file could not be opened " << endl;
			//goi toi method cua CPsola class de mo file database; thaodv-070111
		}
		
		FILE *fp;
		if( (fp = fopen(argv[1], "rt")) == NULL ) 
		{
			cout << "File could not be opened " << argv[1]<<endl;
			return FALSE;
			// lay tham so dau tien truyen vao la ten file text can tong hop; thaodv-070111
			// neu thanh cong tra ve con tro file fp; thaodv-070111
		}

		fseek(fp,0,SEEK_SET); // tro vao dau file; 
		fseek(fp,0,SEEK_END); // tro vao cuoi file thaodv 170211
		
		long lSizeOfFile = ftell(fp);
		fseek(fp,0,SEEK_SET);
		
		char *Buff;
		Buff = new char[lSizeOfFile+1];
			
		fread(Buff,lSizeOfFile,1,fp);
		// doc tu fp, luu vao mot vung nho do con tro Buff tro toi kich thuoc lSizeOfFile*1 bytes thaodv 170211
		Buff[lSizeOfFile] = NULL;

		CString cs; // cs la doan van ban can tong hop
		cs.Format("%s",Buff);//+3);// convert from char array to CString thaodv 170211
		cs.TrimLeft();
		cs.TrimRight();
		//cs.MakeLower();
		
		delete[] Buff;
		fclose(fp);
		
		/*CFile fSynthesis;
	
		//if(!fSynthesis.Open(argv[1],CFile::modeRead))
		if(!fSynthesis.Open("C:\\TDPSOLA_Console\\text.txt",CFile::modeRead|CFile::typeBinary ))
		{
			//cout << "File could not be opened " << argv[1]<< endl;
			cout << "File could not be opened " << endl;
			return FALSE;
		}
		CString cs;
		//Read the infomations contained in the file to CString cs;
		fSynthesis.Read(cs.GetBuffer(fSynthesis.GetLength()),fSynthesis.GetLength());
		cs.ReleaseBuffer();
		
		fSynthesis.Close();*/
		
		CPsola::CsNameOfWavFile		= argv[1];
		CPsola::CsNameOfDebugFile	= CTextHandle::CsDirectoryOfFile + '\\'+ CPsola::CsNameOfWavFile + ".bug";
		CPsola::CsNameOfWavFile		= CTextHandle::CsDirectoryOfFile + '\\'+ CPsola::CsNameOfWavFile + ".wav";
		CPsola::nF0_Begin			= atoi(argv[2]);
		CPsola::fSpeakingRate = atof(argv[3]);
		if(!CPsola::TDPR_PlayText(cs))
		{
			cout << "Can not synthesize text file: " << argv[1] <<endl;
			//cout << "Can not sinthesize text file" << endl;
			return FALSE;
		}
		//CTextHandle::DicSyllableArray.RemoveAll();
		
	
		cout << "The text is synthesized, the sound is stored in the file: " <<argv[1]<<".wav"<< endl;
	}

	return nRetCode;
}


